fix(kafka): avoid recording incorrect partition when using random partitioner#4627
Open
gauravSsinha wants to merge 1 commit into
Open
Conversation
…titioner When no explicit partition and no key are provided, the DefaultPartitioner randomly selects a partition. The instrumentation was calling _partition() to record this value in the span, but the actual send() method calls _partition() again independently, potentially selecting a different partition. This caused the span's messaging.kafka.partition attribute to not match the actual partition where the message was delivered. Fix: only compute partition in the instrumentation when the result is deterministic — either explicitly provided by the caller or determined by key hash. When partition would be randomly assigned, omit the attribute rather than record an incorrect value. Fixes open-telemetry#4625 Signed-off-by: Gaurav Kumar Sinha <gaurav@substrai.dev>
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes incorrect
messaging.kafka.partitionattribute in producer spans when using theDefaultPartitionerwithout an explicit partition or key.Problem
When no explicit partition and no key are provided, the
DefaultPartitionerrandomly selects a partition. The instrumentation callsinstance._partition()to record this value in the span, but the actualsend()method calls_partition()again independently, potentially selecting a different random partition. This causes the span'smessaging.kafka.partitionattribute to not match the actual partition where the message was delivered.Fix
Only compute partition in the instrumentation when the result is deterministic:
messaging.kafka.partitionattribute rather than record an incorrect valueThis is more correct than recording a potentially wrong partition, which could mislead debugging and monitoring.
Changes
KafkaPropertiesExtractor.extract_send_partition()— refactored to only call_partition()when the result is deterministic_enrich_span()— handlesNonepartition gracefully by omitting the attributeFixes #4625